#include <iostream>
#include "body.h"
#include "tiny_render.h"

bool TinyRender::AddBody(Body* body_ptr) {
	const std::lock_guard<std::mutex> lock{ mutex_ };

	if (nullptr == body_ptr) {
		return false;
	}

	for (auto& ptr : body_ptrs_) {
		if (ptr->id() == body_ptr->id()) {
			std::cerr << "Body # " << body_ptr->id()
				<< " already added to GLRender"
				<< std::endl;
			return false;
		}
	}

	body_ptrs_.push_back(body_ptr);
	return true;
}

bool TinyRender::DeleteBody(Body* body_ptr) {
	const std::lock_guard<std::mutex> lock{ mutex_ };

	if (nullptr == body_ptr) {
		return false;
	}

	for (size_t i = 0; i < body_ptrs_.size(); ++i) {
		if (body_ptr->id() == body_ptrs_[i]->id()) {
			body_ptrs_.erase(std::begin(body_ptrs_) + i);
			return true;
		}
	}

	std::cerr << "[ERROR] Can not delete Body # " << body_ptr->id() << std::endl;
	return false;
}

bool TinyRender::RenderSilhouette() {
	if (0 == body_ptrs_.size()) {
		std::cerr << "[ERROR] TinyRender::RenderSilhouette | " 
			"no body to render" << std::endl;
		return false;
	}

	auto& body = body_ptrs_[0];
	auto body2world_pose = body->body2world_pose();
	auto& vertices = body->mesh()->vertices;
	auto& indices = body->mesh()->indices;

	return true;
}

bool TinyRender::RenderNormal() {
	return true;
}